using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
using System.Web;
using System.Xml;
using System;

/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		Alan Benington
	Started:	2004-08-10
	Status:		live
	Version:	1.0.4
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/
	
namespace umlungu.engineX.workflowX {
	/// <summary>
	/// The x_workitems_custom class. 
	/// </summary>
	public class x_workitems_custom : x_workitems {

		#region Constants
		#endregion
		
		#region Invisible properties
		private string customWI;
		#endregion


		#region Constructors/Destructors
		/// <summary>Constructor</summary>
		/// <param name="DSN">the dsn (from web.config)</param>
		public x_workitems_custom(string DSN) : base(DSN) {}
		#endregion

        #region Public methods
        ///	<summary>
        ///	Filtered by the specified agent, state and status 
        /// This method version calls on workitems with join to table with additional data
        /// gets the workitems object with additional element containging all data fields as XML elements
        ///	</summary>
        /// <param name="agentid">The identifier of the 'Agent'</param>
        /// <param name="stateid">The identifier of the 'State'</param>
        /// <param name="statusid">The identifier of the 'Status'</param>
        /// <param name="customWorkItem">The string identifier of the workitem custom process</param>
        public void GetWorkItems(int agentid, int stateid, int statusid, string customWorkItem) {
            ID = agentid;
            customWI = customWorkItem;
            getWorkItems((int)SelectionIdentifier.byAgent, stateid, statusid, 0);
        }
        ///	<summary>
        ///	Filtered by the specified agent, state and status 
        /// This method version calls on workitems with join to table with additional data
        /// gets the workitems object with additional element containging all data fields as XML elements
        ///	</summary>
        /// <param name="stateid">The identifier of the 'State'</param>
        /// <param name="statusid">The identifier of the 'Status'</param>
        /// <param name="proctypeid">The identifier of the 'Process Type'</param>
        /// <param name="proccatid">The identifier of the 'Process Type Category'</param>
        /// <param name="datefrom">The 'From' date</param>
        /// <param name="dateto">The 'To' date</param>
        /// <param name="customWorkItem">The string identifier of the workitem custom process</param>
        /// <param name="fldFilt">The string identifier of the workitem custom process field</param>
        /// <param name="filt">The string identifier of the workitem custom process field filter</param>
        public void GetWorkItems(int stateid, int statusid, int proctypeid, int proccatid, string statDate, string endDate, string customWorkItem, string fldFilt, string filt) {
            customWI = customWorkItem;
            getWorkItems(0, 0, proctypeid, proccatid, stateid, statusid, 0, statDate, endDate, fldFilt, filt);
        }
        ///	<summary>
        ///	Filtered by the specified agent, state and status 
        /// This method version calls on workitems with join to table with additional data
        /// gets the workitems object with additional element containging all data fields as XML elements
        ///	</summary>
        /// <param name="ownerid">The identifier of the 'Owner'</param>
        /// <param name="processid">The identifier of the 'Process'</param>
        /// <param name="stateid">The identifier of the 'State'</param>
        /// <param name="statusid">The identifier of the 'Status'</param>
        /// <param name="proctypeid">The identifier of the 'Process Type'</param>
        /// <param name="proccatid">The identifier of the 'Process Type Category'</param>
        /// <param name="agentid">The identifier of the 'Agent'</param>
        /// <param name="datefrom">The 'From' date</param>
        /// <param name="dateto">The 'To' date</param>
        /// <param name="customWorkItem">The string identifier of the workitem custom process</param>
        /// <param name="fldFilt">The string identifier of the workitem custom process field</param>
        /// <param name="filt">The string identifier of the workitem custom process field filter</param>
        public void GetWorkItems(int ownerid, int processid, int stateid, int statusid, int proctypeid, int proccatid, int agentid, string statDate, string endDate, string customWorkItem, string fldFilt, string filt) {
            customWI = customWorkItem;
            getWorkItems(ownerid, processid, proctypeid, proccatid, stateid, statusid, agentid, statDate, endDate, fldFilt, filt);
        }
		
		///	<summary>
		///	Find any work-item by task or user contact
		///	</summary>
		public void GetWorkItems(int taskid, string contactno, string customWorkItem, string callerid) {
			customWI = customWorkItem;
			getWorkItems(taskid, contactno, callerid);
		}
		///	<summary>
		///	Filtered by the owner, process, state, status and agent
		///	</summary>
		/// <param name="ownerid">The identifier of the 'Owner'</param>
		/// <param name="processid">The identifier of the 'Process'</param>
		/// <param name="agentid">The identifier of the 'Agent'</param>
		/// <param name="stateid">The identifier of the 'State'</param>
		/// <param name="statusid">The identifier of the 'Status'</param>
		/// <param name="datefrom">The 'From' date</param>
		/// <param name="dateto">The 'To' date</param>
		/// <param name="customWorkItem">The string identifier of the workitem custom process</param>
		public void GetWorkItems(int ownerid, int processid, int agentid, int stateid, int statusid, string datefrom, string dateto, string customWorkItem) {
			ID = agentid;
			customWI = customWorkItem;
			getWorkItems(ownerid, processid, stateid, statusid, agentid, datefrom, dateto);
		}
		#endregion
		
		#region Protected methods
		#endregion
				
		#region Private methods
		
		private void getWorkItems(int id, string contact, string callerid) {
			_getCommand("xWorkitemsFind" + customWI, CommandType.StoredProcedure);
			_addDbParameter(id, "@ID");
			_addDbParameter(contact, "@Contact", 30);
			_addDbParameter(callerid, "@Caller", 30);
			SqlDataReader rdr = (SqlDataReader) _executeReader(CommandBehavior.Default);
			addWorkItems(rdr);
			rdr.Close();
        }
        private void getWorkItems(int idOwner, int idProcess, int idState, int idStatus, int idAgent, string dateFrom, string dateTo) {
            _getCommand("xWorkitemsListDateAdditional", CommandType.StoredProcedure);
            _addDbParameter(idOwner, "@OwnerID");
            _addDbParameter(idProcess, "@ProcessID");
            _addDbParameter(idAgent, "@AgentID");
            _addDbParameter(idState, "@StateID");
            _addDbParameter(idStatus, "@StatusID");
            _addDbParameter(dateFrom, "@DateStart", 20);
            _addDbParameter(dateTo, "@DateEnd", 20);
            _addDbParameter(customWI, "@JoinTbl", 50);
            SqlDataReader rdr = (SqlDataReader)_executeReader(CommandBehavior.Default);
            addWorkItems(rdr);
            rdr.Close();
        }
        private void getWorkItems(int idOwner, int idProcess, int idProcessType, int idProcessCat, int idState, int idStatus, int idAgent, string dateFrom, string dateTo, string fld, string fldVal) {
            _getCommand("xWorkitemsListProcessTypeAdditional", CommandType.StoredProcedure);
            _addDbParameter(idOwner, "@OwnerID");
            _addDbParameter(idProcess, "@ProcessID");
            _addDbParameter(idProcessType, "@ProcessTypeID");
            _addDbParameter(idProcessCat, "@ProcessCatID");
            _addDbParameter(idAgent, "@AgentID");
            _addDbParameter(idState, "@StateID");
            _addDbParameter(idStatus, "@StatusID");
            _addDbParameter(dateFrom, "@DateStart", 20);
            _addDbParameter(dateTo, "@DateEnd", 20);
            _addDbParameter(customWI, "@JoinTbl", 50);
            _addDbParameter(fld, "@JoinTblCriteriaFld", 50);
            _addDbParameter(fldVal, "@JoinTblCriteria", 50);
            SqlDataReader rdr = (SqlDataReader)_executeReader(CommandBehavior.Default);
            addWorkItems(rdr);
            rdr.Close();
        }
		private void getWorkItems(int idType, int idState, int idStatus, int idAgent) {
			_getCommand("xWorkitemsSelectAdditional", CommandType.StoredProcedure);
			_addDbParameter(ID, "@ID");
			_addDbParameter(idType, "@IDType");
			_addDbParameter(idAgent, "@AgentID");
			_addDbParameter(idState, "@StateID");
			_addDbParameter(idStatus, "@StatusID");
			_addDbParameter(customWI, "@JoinTbl", 50);
			SqlDataReader rdr = (SqlDataReader) _executeReader(CommandBehavior.Default);
			addWorkItems(rdr);
			rdr.Close();
		}
		
		private void addWorkItems(SqlDataReader rdr) {
			string colName;
			string addItemData = customWI;
			int colCnt = rdr.FieldCount;
			x_workflow workflowx = new x_workflow();

			while (rdr.Read()) {
				XmlElement thisitem = workitems.CreateElement("workitem");
				string agentid = _getString(rdr, "AgentID");
				_addAttribute(workitems, thisitem, "id", _getString(rdr, "ID"));
				_addAttribute(workitems, thisitem, "process", _getString(rdr, "ProcessID"));
				_addElement(workitems, thisitem, "Process", _getString(rdr, "ProcessDescription"), "name", _getString(rdr, "ProcessName"));
				_addAttribute(workitems, thisitem, "agent", agentid);
				_addElement(workitems, thisitem, "Agent", String.Concat(_getString(rdr, "FirstName"), " ", _getString(rdr, "Surname")), "email", _getString(rdr, "EMail"));
				_addAttribute(workitems, thisitem, "state", _getString(rdr, "StateID"));
				_addElement(workitems, thisitem, "State", _getString(rdr, "StateDescription"), "name", _getString(rdr, "StateName"));
				_addAttribute(workitems, thisitem, "status", _getString(rdr, "StatusID"));
				_addElement(workitems, thisitem, "Status", _getString(rdr, "StatusDescription"), "name", _getString(rdr, "StatusName"));
				_addElement(workitems, thisitem, "startdate", _getDate(rdr, "StartDate"));
				_addElement(workitems, thisitem, "enddate", _getDate(rdr, "EndDate"));
				_addElement(workitems, thisitem, "user", "", "ID", _getString(rdr, "UserID"));
				XmlElement userel = (XmlElement) thisitem.SelectSingleNode("user");
				_addElement(workitems, userel, "name", _getString(rdr, "UserName"));
				_addElement(workitems, userel, "contact", _getString(rdr, "UserContact"));
				_addElement(workitems, userel, "email", _getString(rdr, "UserEmail"));
				_addElement(workitems, thisitem, "Request", _getString(rdr, "Request"));
				// SS 20070330 : Appends an additional element containining all columns returned as <Add><[column_name]>Column_Value</[column_name]></Add>
				_addElement(workitems, thisitem, "Add", "");
				XmlElement additional = (XmlElement) thisitem.SelectSingleNode("Add");
				for (int iCol = 0; iCol < colCnt; iCol ++ ){
					colName = rdr.GetName(iCol).ToString();
					if(colName.IndexOf(addItemData)!=-1){
						_addElement(workitems, additional, colName, _getString(rdr, colName));
					}
				}
				// SS 20070330
				WorkItems.AppendChild(thisitem);
			}
		}
		#endregion

	}
}
